package page;

import java.awt.TextComponent;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;

import app.WebApp;
import dao.AdminDao;
import dao.FlightDao;
import entity.Admin;
import entity.Flight;

public class AdminLoginPage extends WebPage {
	
	public AdminLoginPage(){
		super();
		final Admin admin = new Admin();
		final Form<Admin> form = new Form<Admin>("form", new CompoundPropertyModel<Admin>(admin));
		add(form);
		form.add(new TextField<String>("login"));
		form.add(new TextField<String>("password"));
		form.add(new SubmitLink("loginButton") {
			@Override
			public void onSubmit() {
				if(admin.getLogin()==null || admin.getPassword()==null){
					setResponsePage(new AdminLoginPage());
				}
				else {
					final AdminDao adminDao = new AdminDao(WebApp.DATA_XML_FOLDER);
					final List<Admin> Admins = adminDao.getAll();
					if(Admins.size()==0){
						Admin newAdmin = new Admin();
						newAdmin.setLogin("admin");
						newAdmin.setPassword("admin");
						adminDao.saveNew(newAdmin);
					}
					final List<Admin> allAdmins = adminDao.getAll();
					for(final Admin row:allAdmins){
						if(admin.getLogin().equals(row.getLogin()) && admin.getPassword().equals(row.getPassword())){
							setResponsePage(new AdminFlightListPage());
							break;
						}
						else setResponsePage(new AdminLoginPage());
					}
				}
				
			}
		});
		form.add(new SubmitLink("cancel") {
			@Override
			public void onSubmit() {
				setResponsePage(new AllFlightListPage());
			}
		});
	};		
}

